/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blink.model;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Cette classe modélise un Essai.
 * Un essai est généré à partir d'une tâche en fonction des paramètres de la 
 * tache (distracteurs, cibles, temps de latence, et autres contraintes). 
 * Cette classe représente la liste des stimuli générés, ainsi que les réponses 
 * données par l'utilisateur et les réponses attendues.
 * @author Jerome David
 */
public class Trial implements Serializable{
    
	//reponse aux questions
	public final static int NO=-1;
    public final static int YES=-2;
    public final static int NO_ANSWER=-3;
    
   //task assoxccie a l'essai
    private Task task;
    private Stimulus[] sequence;
    
    //tableau reponse
    private int[] userResponses;
    private int[] correctResponses;
    
    public Trial(Task t) {
        task=t;
        generateTrial();
    }
    
    private void generateTrial() {
        
        // Generate a sequence of random size (between possibleLengths)
        int length = task.getPossibleSequenceLengths()[(int) (Math.random()*task.getPossibleSequenceLengths().length)];
        sequence = new Stimulus[length];
        boolean found;
        for (int i=0 ; i<sequence.length ; i++) {
            Stimulus candidate;
            do {
                candidate = task.getFillers().get((int) (Math.random()*task.getFillers().size()));
                found=false;
                for (int j=1 ; i-j>-1 && j<= task.getMinGapBetweenFillers(); j++) {
                    if (candidate==sequence[i-j]) {
                        found=true;
                        break;
                    }
                }
               
            }
            while (found);
            sequence[i]=candidate;
        }
        
        correctResponses = new int[task.getTargets().size()];
        userResponses = new int[task.getTargets().size()];
        Arrays.fill(userResponses, NO_ANSWER);
        
        int i=0;
        // should be done before
        for (Target t : task.getTargets()) {
            if (Math.random()<t.getProbability()) {
                int position = Integer.MAX_VALUE;
                while (position>=sequence.length) position=t.getPossiblePositions()[(int) (Math.random()*t.getPossiblePositions().length)];
                int idxTarget =(int) (Math.random()*t.getPossibleTargets().size());
                sequence[position]=t.getPossibleTargets().get(idxTarget);
                if (t.isDetected())
                    correctResponses[i++]=Trial.YES;
                else
                    correctResponses[i++]=idxTarget;
            }
            else 
                correctResponses[i++]=Trial.NO;
        } 
    }  
    
    
    public void setUserResponse(int pos, int response) {
        userResponses[pos]=response;
    }
    
    
    //renvoie un tableau de reponses
    public int[] getUserResponses() {
        return userResponses;
    }
    
    public int[] getCorrectResponses() {
        return correctResponses;
    }

    public Stimulus[] getSequence() {
        return sequence;
    }

    public void setSequence(Stimulus[] sequence) {
        this.sequence = sequence;
    }

    public Task getTask() {
        return task;
    }
    
    
}
